﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.Data.Filtering;
using DevExpress.Export;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraLayout;
using XCI.Extensions;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting.Controls
{
    public partial class SettingGridGeneral : BaseUserControl
    {
        private GridView gridView;
        public SettingGridGeneral()
        {
            InitializeComponent();
        }

        public void Init(GridView gridView)
        {
            this.gridView = gridView;
            this.accordion.SelectedElement = this.elementBehavior;
            WinFormHelper.FillGridViewColumn(gridView, comboBoxField);
            WinFormHelper.AddEnum(typeof(DefaultBoolean), editAllowMergedGrouping, editAllowAddRows,
                editAllowDeleteRows, editAllowFixedGroups, editAllowGroupExpandAnimation,
                editAllowCopy, editCopyColumnHeaders, editCopyCollapsedData, comboBoxHotTrackedRow,
                comboBoxShowCheckBoxSelectorInColumnHeader, comboBoxShowCheckBoxSelectorInGroupRow,
                comboBoxShowCheckBoxSelectorInPrintExport, editShowHoriLines, editShowVerticalLines);

            WinFormHelper.AddEnum(typeof(GridMultiSelectMode), comboBoxMultiSelectMode);
            WinFormHelper.AddEnum(typeof(GridEditingMode), editEditingMode);
            WinFormHelper.AddEnum(typeof(EditorShowMode), editEditorShowMode);
            WinFormHelper.AddEnum(typeof(PasteMode), editPasteMode);
            WinFormHelper.AddEnum(typeof(ClipboardMode), editClipboardMode);
            WinFormHelper.AddEnum(typeof(FindPanelBehavior), editBehavior);
            WinFormHelper.AddEnum(typeof(FilterCondition), editCondition);
            WinFormHelper.AddEnum(typeof(FindPanelParserKind), editParserKind);
            WinFormHelper.AddEnum(typeof(GridAnimationType), editAnimationType);
            WinFormHelper.AddEnum(typeof(WaitAnimationOptions), editWaitAnimationOptions);
            WinFormHelper.AddEnum(typeof(GroupDrawMode), editGroupDrawMode);
            WinFormHelper.AddEnum(typeof(GroupFooterShowMode), editGroupFooterShowMode);
            WinFormHelper.AddEnum(typeof(NewItemRowPosition), editNewItemRowPosition);
            WinFormHelper.AddEnum(typeof(ShowButtonModeEnum), editShowButtonMode);
            WinFormHelper.AddEnum(typeof(DrawFocusRectStyle), boxFocusRectStyle);
            WinFormHelper.AddEnum(typeof(ScrollVisibility), boxHorzScrollVisibility, boxVertScrollVisibility);
            WinFormHelper.AddEnum(typeof(BorderStyles), editBorderStyles);
            WinFormHelper.BindCustomPropertyControlValue(layoutControl1, gridView);

            editShowHoriLines.EditValue = gridView.OptionsView.ShowHorizo​​ntalLines;
            editShowHoriLines.EditValueChanged += (o, e) =>
            {
                gridView.OptionsView.ShowHorizo​​ntalLines = (DefaultBoolean)Enum.Parse(typeof(DefaultBoolean), editShowHoriLines.EditValue.ToStringOrEmpty());
            };

            btnCleanSort.Enabled = gridView.SortedColumns.Count > 0;
            btnCleanFilter.Enabled = gridView.ActiveFilter.Count > 0;
            btnCleanGroup.Enabled = gridView.GroupedColumns.Count > 0;
        }

        private void SettingGridGeneral_Load(object sender, EventArgs e)
        {
            UpdateCurrentOptionsVisibility();
        }

        private void Accordion_SelectedElementChanged(object sender, DevExpress.XtraBars.Navigation.SelectedElementChangedEventArgs e)
        {
            UpdateCurrentOptionsVisibility();
        }

        void UpdateCurrentOptionsVisibility()
        {
            if (accordion.SelectedElement?.Tag == null) return;
            var name = accordion.SelectedElement.Tag.ToString();
            var item = layoutControl1.Items.FindByName("layoutGroup" + name);
            if (item != null)
            {
                item.Owner.Scroller.VScroll.Value = item.Bounds.Y;
                //var item = layoutControl1.Items.FindByName("layoutControlGroup3");
                //layoutControl1.FocusHelper.PlaceItemIntoView(layoutControlGroup3);
            }
        }

        private void btnCleanSort_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要清除列排序吗？") == DialogResult.Yes)
            {
                gridView.ClearSorting();
                btnCleanSort.Enabled = false;
            }
        }

        private void btnCleanGroup_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要清除分组列吗？") == DialogResult.Yes)
            {
                gridView.ClearGrouping();
                btnCleanGroup.Enabled = false;
            }
        }

        private void btnCleanFilter_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要清除列过滤吗？") == DialogResult.Yes)
            {
                gridView.ClearColumnsFilter();
                btnCleanFilter.Enabled = false;
            }

            ////切换到指定的面板
        }
    }
}
